package contacts;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 * Klasse soll vCards einlesen und in einem FileArray bereitstellen
 * 
 * @author Michi
 * 
 */
public class Importer {

	public Importer() {

	}

	/**
	 * Konstruktor - Oeffnet eine Dateiauswahl fuer vCards und speichert die
	 * ausgewaehlten Dateien in ein File Array
	 * 
	 * @return File Array welches vCards beinhaltet
	 */
	public File[] getFiles() {
		JFileChooser chooser = new JFileChooser();
		chooser.setDialogTitle("Select VCards");
		chooser.setCurrentDirectory(new File(System.getProperties()
				.getProperty("user.home")));
		chooser.setMultiSelectionEnabled(true);
		chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
			@Override
			public boolean accept(File f) {
				return f.getName().toLowerCase().endsWith(".vcf")
						|| f.isDirectory();
			}

			public @Override
			String getDescription() {

				return "VCard Files";
			}
		});

		int result = chooser.showOpenDialog(null);
		if (result == JFileChooser.CANCEL_OPTION) {
			return null;
		}

		try {
			File[] files = chooser.getSelectedFiles(); // get the file
			return files;
		} catch (Exception ex) {
			JOptionPane.showMessageDialog(null,
					"Warning! Could not load the file(s)!", "Warning!",
					JOptionPane.WARNING_MESSAGE);
			return null;
		}
	}

}
